<!doctype html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml" xmlns:v-model="http://www.w3.org/1999/xhtml" xmlns:v-demo="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .router-link-active {
            color:#f00;
        }
        pre {
            font-family: sans-serif;
            font-size: 1em;
            line-height:1.5em;
        }

    </style>
</head>
<body>

    <div id="app">
        <ul>
            <li><router-link to="/login">Login</router-link></li>
            <li><router-link to="/foo">Foo</router-link></li>
        </ul>
        <router-view></router-view>
    </div>
</body>

<script src="../../../vender/vue@2.4.2.js"></script>
<script src="../../../vender/vue-router@2.7.0.js"></script>

<script>

    const Foo={
        template:`<div>
            <h6>Foo</h6>
            <router-link to="/foo/bar">/foo/bar</router-link>
            <router-view></router-view>
        </div>`
    };
    const Bar={
        template:`<div>
            <h6>Bar</h6>
        </div>`
    };
    const Login={
        template:`<div>
            <h6>Login</h6>
        </div>`
    };

    const router=new VueRouter({
        routes:[
            {path:'/login',component:Login},
            {
                path:'/foo',component:Foo,
                children:[
                    {
                        path:'bar',component:Bar,
                        meta:{requiresAuth:true}
                    }
                ]
            }
        ]

    });
    router.beforeEach((to,from,next)=>{
        console.log('to:',to);
        console.log('from:',from);
        if(to.matched.some(record=>record.meta.requiresAuth)){
            next({
                path:'/login',
                query:{redirect:to.fullPath}
            });
        }else{
            next();
        }
    });

    const app=new Vue({
        el:'#app',
        router
    });

</script>
</html>